<?php
include_once('../lib/function.php');
/*
$listfile = 'emule_all/avlist.html';
$listfile = file_get_contents($listfile);

preg_match_all('/<table class="wikitable sortable jquery-tablesorter"[^>]*>(.*)<\/table>/is', $listfile, $matches);
$listfile = $matches[0][0];

preg_match_all('/<tbody>(.*)<\/tbody>/is', $listfile, $matches);
$listfile = $matches[0][0];
$listfile = preg_replace('/\s+/', '', $listfile);

preg_match_all('/<tr>.*?<\/tr>/i', $listfile, $matches);

$data = [];
foreach ($matches[0] as $item) {
	preg_match_all('/<td>(.*?)<\/td>/i', $item, $matches2);
	$tmp = [];
	foreach ($matches2[1] as $k=>$value) {
		$value = strip_tags($value);
		if($k == 0) {
			$value = preg_replace('/（.*?）/', '', $value);
			$value = preg_replace('/\[.*?\]/', '', $value);
		}

		$tmp[] = $value;
	}

	$data[] = $tmp;
}

echo(json_encode($data));
*/
$json = json_decode(file_get_contents('emule.json'), true);

foreach ($json as $k=>$item) {
	echo2($k, 'loop.start');
	if(empty($item[0])) continue;

	$savefile = 'emule_all/search/'.$item[0].'.html';
	if(file_exists($savefile)) continue;

	$shell = "/usr/bin/casperjs emule.js '".$item[0]."'";
	echo2($shell, 'shell.command');
    $result = shell_exec($shell);
    echo2($result, 'shell.result');

    //break;
}
